package com.poker.server;
import java.util.Iterator;
import java.util.LinkedList;
import com.poker.client.LoginInfo;
import com.poker.client.LoginService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.XsrfProtectedServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public class LoginServiceImpl extends RemoteServiceServlet implements
LoginService {
private static final long serialVersionUID = 1L;
ChannelService channelService = ChannelServiceFactory.getChannelService();
static {
// register entity
ObjectifyService.register(MatchInfo.class);
ObjectifyService.register(PlayerInfo.class);
ObjectifyService.register(GamePeriodData.class);
ObjectifyService.register(OtherPlayerInfo.class);
}
@Override
public LoginInfo loginWithFBID(String nameAndId) {
LoginInfo loginInfo = new LoginInfo();
//judge if user exists
if (nameAndId!= null) {
String[] names=nameAndId.split("@");
loginInfo.setLoggedIn(true);
loginInfo.setFbId(names[1]);
loginInfo.setNickname(names[0]);
String token = channelService.createChannel(loginInfo.getFbId());
loginInfo.setToken(token);
} else {
loginInfo.setLoggedIn(false);
}
return loginInfo;
}
@Override
public OtherPlayerInfo getIdInfo(String userId, String opponentId) { // Load Player Info by FB Id
OtherPlayerInfo other = new OtherPlayerInfo();
PlayerInfo saved = DataOperation.getPlayer(opponentId);
if (saved == null) {
other.setRank(1500);
other.setrd(350);
} else {
int rank = saved.rank;
int rd=saved.rd;
other.setRank(rank);
other.setrd(rd);
MatchInfo game = LoadMatchByOpponentInDropdown(userId, opponentId);
if (game == null) {
other.setExist(0);
} else {
other.setExist(1);
other.setGameId(game.getMatchId());
// Turn: User Turn = 1, Opponent Turn = 2
if (game.getTurn().equals(userId))
other.setTurn(1);
else
other.setTurn(2);
}
}
return other;
}
@Override
public MatchInfo LoadMatchByOpponentInDropdown(String userId, String opponentId) {
Objectify ofy = ObjectifyService.ofy();
Iterable<MatchInfo> list = ofy.load().type(MatchInfo.class);
Iterator<MatchInfo> itr= list.iterator();
while(itr.hasNext()){
MatchInfo mt = itr.next();
if(mt.getPlayerOneEmail().equals(userId)&&mt.getPlayerTwoEmail().equals(opponentId)||mt.getPlayerOneEmail().equals(opponentId)&&mt.getPlayerTwoEmail().equals(userId))
// found the match!
return mt;
}
return null;
}
}